package ca.ubc.icapture.genapha.actions;

import ca.ubc.icapture.genapha.forms.VariableCategoriesForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.Action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import icapture.SQLMgr;
import icapture.beans.DB.Category;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: btripp
 * Date: Sep 13, 2006
 * Time: 9:39:08 AM
 * To change this template use File | Settings | File Templates.
 */
public class VariableCategoriesAction extends Action
{

    @Override
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
    {

        ActionForward forward = new ActionForward();
        VariableCategoriesForm vform = (VariableCategoriesForm) form;

        String categoryID = vform.getCategoryID();
        if (categoryID == null || categoryID.length() == 0)
        {
            //display root categories
            forward = mapping.findForward("categoryList");
            vform.setCategoryList(SQLMgr.getCategories(null));

        }
        else
        {
            //chech if there are subcategories to display
            ArrayList<Category> categoryList = null;
            try
            {
                categoryList = SQLMgr.getCategories(Integer.parseInt(categoryID));
            }
            catch (Exception e)
            {
            }


            //if a category has mixed categories/variable display as separate items
            //If its just variables display as tables of variables
            if (categoryList != null && categoryList.size() > 0)
            {
                //display sub categories or display variable list
                boolean isVars = true;
                for (Category cat : categoryList)
                {
                    ArrayList<Category> subCategoryList = SQLMgr.getCategories(cat.getCategoryID());
                    if (subCategoryList.size() > 0)
                    {
                        isVars = false;
                        break;
                    }
                }
                if (isVars)
                {
                    forward = mapping.findForward("variableList");
                }
                else
                {
                    forward = mapping.findForward("categoryList");
                }

                vform.setCategoryList(categoryList);
            }
            else
            {
                //display results for single variable
                forward = mapping.findForward("variableList");
                ArrayList<Category> vdList = new ArrayList<Category>();
                vform.setCategoryList(vdList);
                try
                {
                    vdList.add(SQLMgr.getCategory(Integer.parseInt(categoryID)));
                }
                catch (Exception e)
                {
                }
            }
        }
        return forward;
    }
}
